package com.wehealth;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.umeng.analytics.MobclickAgent;
import com.wehealth.adapter.Adapter_PlanDay_new;
import com.wehealth.dm.DM_PlanDay_new;
import com.wehealth.dm.User;
import com.wehealth.util.DateUtil;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

@SuppressLint("ValidFragment")
public class Plan_Day_record extends Fragment {
	MainActivity activity;
	private static  String JSONResult;
	private static final int NET_ERROR = -100;
	
	private EditText  mHighValueEt;
	private EditText  mLowValueEt;
	private User user;
	private DM_PlanDay_new mPlanItem;
	
	
	public  Plan_Day_record()
	{

	}
	
	public Plan_Day_record(DM_PlanDay_new mItem){
		mPlanItem =mItem;
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.plan_day_blood_sugar, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		
		super.onActivityCreated(savedInstanceState);
	    activity = (MainActivity)getActivity();
	    user = activity.getUserInfo();
	    TextView title =(TextView)activity.findViewById(R.id.plan_day_record_title);
	    String name = mPlanItem.getPlanItemTypeName();
	    title.setText(name);
	    
	    mHighValueEt =(EditText)activity.findViewById(R.id.plan_day_record_row1_value_et);
	    mLowValueEt =(EditText)activity.findViewById(R.id.plan_day_record_row2_value_et);
	    
	    TextView mHighTv =(TextView)activity.findViewById(R.id.plan_day_record_row1_name_tv);
	    mHighTv.setText(getString(R.string.play_day_record_high_value));
	    TextView mLowTv =(TextView)activity.findViewById(R.id.plan_day_record_row2_name_tv);
	    mLowTv.setText(getString(R.string.play_day_record_low_value));
		//返回主页按钮-------------------------------------------------------//
	    ImageView bBack = (ImageView) activity.findViewById(R.id.plan_day_record_back);
			
		 bBack.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
				    activity.switchContentHome();
				}
			});
			
		//------------------------------------------------------------------//
		 
		 Button submitBt=(Button)activity.findViewById(R.id.plan_day_record_submit_bt);
		 submitBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				connectServierSubmitData();
			}
		});
		 
		
	}
	
	
	

	/**
	 * 渲染界面,显示数据
	 */
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message paramMessage) {
			//取消加载框
    		if(JSONResult!=null){
    			ResultFromServer(JSONResult);
    		}else{
    			 activity.openDialogBt(NET_ERROR); 
    		}
    		JSONResult =null;
		}
	};

	private void connectServierSubmitData(){
		
		new Thread(){
			@Override
			public void run(){
				try {
					SubmitDataToServer();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			handler.sendEmptyMessage(0);
			}
		}.start();
	}
	
	
	
	private void  SubmitDataToServer() throws IOException
	{
		String userID =String.valueOf(user.getUserId());
		String  mLowValue = mLowValueEt.getText().toString().trim();
		String  mHighValue = mHighValueEt.getText().toString().trim();
	
		String httpUrl = com.wehealth.util.GetUrl.Monitor_RecordingBP_URL();
		 HttpPost httpRequest= new HttpPost(httpUrl);
		 
		 List <NameValuePair> params = new ArrayList <NameValuePair>();
		 
		 String planItemID =mPlanItem.getID();
		 
		 params.add(new BasicNameValuePair("userID",userID));
		 params.add(new BasicNameValuePair("min",mLowValue)); 
		 params.add(new BasicNameValuePair("max",mHighValue)); 
		 params.add(new BasicNameValuePair("planItemID",planItemID)); 
		 Log.d("SIMMON","Plan_Day  userID="+userID);
		 try
		 {
			 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8) );
			
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			 
			 if(httpResponse.getStatusLine().getStatusCode() == 200)
			 {
				 JSONResult = EntityUtils.toString(httpResponse.getEntity());
				 Log.d("SIMMON","Plan_Day  JSONResult="+JSONResult);
			 }
		 }
		 catch(Exception e)
		 {
			
			 Log.e("error:",  e.toString());
			 
		 }

	}
	
	
    //将JSON数据绑定到LISTVIEW
	   private void ResultFromServer(String strJSON){
	       try{
	                JSONObject jsonObject = new JSONObject(strJSON); 
	                String  result =   jsonObject.getString("result");
	               if(result.equals("1")){
						    String dialog = getString(R.string.play_day_info_do_flag_sucess);
							activity.openDialog(dialog,"好的");
	               }else{
	            	   activity.openDialogBt(NET_ERROR); 
	               }
	            }
	            catch(JSONException ex)
	            {
	                    Log.e("JSON Error: ", ex.toString()); 
	            }
	    }
	
  
}
